package com.xjy.web.controller.system;

import com.xjy.common.annotation.Log;
import com.xjy.common.annotation.RepeatSubmit;
import com.xjy.common.config.xjyConfig;
import com.xjy.common.core.controller.BaseController;
import com.xjy.common.core.domain.AjaxResult;
import com.xjy.common.core.domain.entity.SysUser;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.enums.BusinessType;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.file.FileUploadUtils;
import com.xjy.common.utils.poi.ExcelUtil;
import com.xjy.system.domain.SysContract;
import com.xjy.system.domain.bo.SysContractAddBo;
import com.xjy.system.domain.bo.SysContractEditBo;
import com.xjy.system.domain.bo.SysContractQueryBo;
import com.xjy.system.domain.vo.SysContractVo;
import com.xjy.system.service.ISysContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static com.xjy.common.utils.SecurityUtils.getLoginUser;

/**
 * 合同基本信息Controller
 *
 * @author xjy
 * @date 2022-02-08
 */
@Api(value = "合同基本信息控制器", tags = {"合同基本信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/contract")
public class SysContractController extends BaseController {

	private final ISysContractService iSysContractService;

	/**
	 * 查询合同基本信息列表
	 */
	@ApiOperation("查询合同基本信息列表")
	@PreAuthorize("@ss.hasPermi('system:contract:list')")
	@GetMapping("/list")
	public TableDataInfo<SysContractVo> list(@Validated SysContractQueryBo bo) {
		startPage();
		return iSysContractService.queryPageList(bo);
	}

	/**
	 * 查询合同基本信息列表
	 */
	@ApiOperation("查询合同基本信息列表")
	@PreAuthorize("@ss.hasPermi('system:contract:list')")
	@GetMapping("/selectList")
	public List<SysContractVo> selectList(@Validated SysContractQueryBo bo) {


		return iSysContractService.selectList(bo);
	}

	/**
	 * 导出合同基本信息列表
	 */
	@ApiOperation("导出合同基本信息列表")
	@PreAuthorize("@ss.hasPermi('system:contract:export')")
	@Log(title = "合同基本信息", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<SysContractVo> export(@Validated SysContractQueryBo bo) {
		List<SysContractVo> list = iSysContractService.queryList(bo);
		ExcelUtil<SysContractVo> util = new ExcelUtil<>(SysContractVo.class);
		return util.exportExcel(list, "合同基本信息");
	}

	/**
	 * 获取合同基本信息详细信息
	 */
	@ApiOperation("获取合同基本信息详细信息")
	@PreAuthorize("@ss.hasPermi('system:contract:query')")
	@GetMapping("/{id}")
	public AjaxResult<SysContractVo> getInfo(@NotNull(message = "主键不能为空")
											 @PathVariable("id") Long id) {
		return AjaxResult.success(iSysContractService.queryById(id));
	}

	/**
	 * 新增合同基本信息
	 */
	@ApiOperation("新增合同基本信息")
	@PreAuthorize("@ss.hasPermi('system:contract:add')")
	@Log(title = "合同基本信息", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody SysContractAddBo bo) {
		SysUser user = getLoginUser().getUser();
		bo.setUserId(user.getUserId());
		return toAjax(iSysContractService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改合同基本信息
	 */
	@ApiOperation("修改合同基本信息")
	@PreAuthorize("@ss.hasPermi('system:contract:edit')")
	@Log(title = "合同基本信息", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/edit")
	public AjaxResult<Void> edit(@Validated @RequestBody SysContractEditBo bo) {
		SysContractVo sysContractVo = iSysContractService.queryById(bo.getId());
		if (sysContractVo.getAuditStatus()) {
			throw new UtilException("该数据已经审核无法修改！");
		}
		return toAjax(iSysContractService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 修改合同基本信息
	 */
	@ApiOperation("修改合同基本信息")
	@PreAuthorize("@ss.hasPermi('system:contract:edit')")
	@Log(title = "合同基本信息", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/audit")
	public AjaxResult<Void> audit(@Validated @RequestBody SysContractEditBo bo) {
		SysContractVo sysContractVo = iSysContractService.queryById(bo.getId());
		if (sysContractVo.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}


		return toAjax(iSysContractService.auditByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除合同基本信息
	 */
	@ApiOperation("删除合同基本信息")
	@PreAuthorize("@ss.hasPermi('system:contract:remove')")
	@Log(title = "合同基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iSysContractService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}

	/**
	 * 上传合同映像
	 */
	@Log(title = "上传合同映像", businessType = BusinessType.UPDATE)
	@PostMapping("/uploadContractImg")
	public AjaxResult uploadContractImg(@RequestParam("avatarfile") MultipartFile file) throws IOException {
		if (!file.isEmpty()) {
			String image = FileUploadUtils.upload(xjyConfig.getUploadPath(), file);
			if (StringUtils.isNotEmpty(image)) {
				return AjaxResult.success(image);
			}
		}
		return AjaxResult.error("上传图片异常，请联系管理员");
	}

	/**
	 * 根据项目查询合同
	 */
	@ApiOperation("根据项目查询合同")
	@GetMapping("/contractSByProj/{id}")
	public List<SysContract> listByProj(@NotNull(message = "主键不能为空")
										@PathVariable("id") Long id) {
		return iSysContractService.listByProj(id);
	}

}
